var HxBasicGoodsInfoDlg = {
    data: {
		id:"",
        goodsCustomerId: "",
        goodsCustomerName: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsXcode69: "",
        goodsHelpcode: "",
        goodsSpecs: "",
        goodsBigtype: "",
        goodsBrand: "",
        jibenUnit: "",
        kcUnit: "",
        kcRate: "",
        cgUnit: "",
        cgRate: "",
        pfUnit: "",
        pfRate: "",
        psUnit: "",
        psRate: "",
        anquanNum: "",
        goodsBottom: "",
        goodsLamination: "",
        goodsTraylayer: "",
        goodsSupportvolume: "",
        length: "",
        height: "",
        width: "",
        goodsWeight: "",
        goodsVolume: "",
        goodsChargingCg: "",
        goodsChargingPs: "",
        goodsChargingPf: "",
        goodsWarranty: ""
    }
};
var BasicPutgoodsInfoDlg = {
    data: {
		id:"",
		dictId:"",
        putgoodsCustomerId: "",
        putgoodsOperateName: "",
        putgoodsName: "",
		unitNumber:"",
		unitType:""
    }
};
layui.use(['table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startTime'
        	,value:new Date()
    });
    
    laydate.render({
    	elem:'#endTime'
        	,value:new Date()
    });

    /**
     * 拣货单明细表管理
     */
    var HxStorageOutDetailed = {
        tableId: "hxStorageOutDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    HxStorageOutDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'setWarehouse', sort: true, title: '发货仓库'},
            {field: 'getUnito', sort: true, title: '收货客户'},
            {field: 'areaName', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'storageNumber', sort: true, title: '拣货单编号', width: 140},
            {field: 'storageState', sort: true, title: '单据状态', templet: '#storageStateTpl'},
            {field: 'startState', sort: true, title: '发车状态', templet: '#startStateTpl'},
            {field: 'goodsName', sort: true, title: '商品', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'goodsPcode69', sort: true, title: '商品码', hide: true},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'unit', sort: true, title: '单位', totalRowText: '合计：'},

            {field: 'num', sort: true, title: '数量', totalRowText: sl.num},
            {field: 'realNum', sort: true, title: '实拣数量', totalRowText: sl.realNum},
            {field: 'qualityNum', sort: true, title: '复核数量', totalRowText: sl.qualityNum},
            {field: 'outWeight', sort: true, title: '应出重量', totalRowText: sl.outWeight, hide: true},
            {field: 'outVolume', sort: true, title: '应出体积', totalRowText: sl.outVolume, hide: true},
            {field: 'outMoney', sort: true, title: '金额', totalRowText: sl.outMoney, hide: true},
            {field: 'detailedDate', sort: true, title: '生产日期', hide: true},

            {field: 'trayNumber', sort: true, title: '托盘编号'},
            {field: 'stockPort', sort: true, title: '集货通道'},
            {field: 'storageUserName', sort: true, title: '分拣员'},
            {field: 'storageTimeo', sort: true, title: '分拣开始时间'},
            {field: 'storageCompleteTime', sort: true, title: '分拣完成时间'},
            {field: 'qualityUserName', sort: true, title: '复核员'},
            {field: 'qualityTime', sort: true, title: '复核开始时间'},
            {field: 'qualityCompleteTime', sort: true, title: '复核完成时间'},
            {field: 'nodes', sort: true, title: '备注'}
        ]];
    };

    var sl = {
        num:"",
        realNum:"",
        qualityNum:"",
        outWeight:"",
        outVolume:"",
        outMoney:""
    }

    //点击选择商品名称
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
            }
        });
    });
    
    $('#choiceGetUnit').click(function () {
        layer.open({
            type: 2,
            title: '选择单位',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPutgoods/tochoiceBasicPutgoods_hx',
            end: function () {
                $("#getUnit").val(BasicPutgoodsInfoDlg.data.putgoodsName);
            }
        });
    });

    $('#reset').click(function () {
        $("#areaName").val("")
        $("#positionNumber").val("")
        $("#getUnit").val("")
        $("#condition").val("")
        $("#associationOrder").val("")
        $("#goodsName").val("")
        $("#trayNumber").val("")
        $("#storageUserName").val("")
        $("#qualityUserName").val("")
    });

    /**
     * 点击查询按钮
     */
    HxStorageOutDetailed.search = function () {
        var queryData = {};
        queryData['timeType'] = $("#timeType").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['setWarehouseId'] = $("#setWarehouseId").val();
        queryData['areaName'] = $("#areaName").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['getUnits'] = $("#getUnit").val();
        queryData['condition'] = $("#condition").val();
        queryData['associationOrder'] = $("#associationOrder").val();
        queryData['goodsNames'] = $("#goodsName").val();
        queryData['trayNumber'] = $("#trayNumber").val();
        queryData['storageUserName'] = $("#storageUserName").val();
        queryData['qualityUserName'] = $("#qualityUserName").val();

	    $.ajax({
	        url:Feng.ctxPath + '/hxStorageOutDetailed/count',
            data: queryData, // 将 queryData 作为请求体发送
	        dataType:"json",   //返回格式为json
	        async: false,
	        type:"post",   //请求方式
	        success:function(data){
	            sl = data
	        }
	    });
	
	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + HxStorageOutDetailed.tableId,
            where: queryData,
	        url: Feng.ctxPath + '/hxStorageOutDetailed/jilulist',
	        height: "full-60",
	        toolbar: '#toolbarfrom',
	        cellMinWidth: 100,
	        totalRow: true ,//开启合计行
	        cols: HxStorageOutDetailed.initColumn()
            ,parseData: function(res){ // 数据转换
                return {
                    "code": 0,
                    "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                    "count": res.count,
                    "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                        return {
                            // 假设你想转换的字段是numberField
                            realNum: Number(item.realNum), // 将字符串转换为数字
                            qualityNum: Number(item.qualityNum),
                            outWeight: Number(item.outWeight),
                            outVolume: Number(item.outVolume),
                            id: item.id,
                            setWarehouse: item.setWarehouse,
                            getUnito: item.getUnito,
                            areaName: item.areaName,
                            positionNumber: item.positionNumber,
                            storageNumber: item.storageNumber,
                            storageState: item.storageState,
                            startState: item.startState,
                            goodsName: item.goodsName,
                            goodsNumber: item.goodsNumber,
                            goodsPcode69: item.goodsPcode69,
                            goodsSpecs: item.goodsSpecs,
                            unit: item.unit,
                            num: item.num,
                            outMoney: Number(item.outMoney),
                            detailedDate: item.detailedDate,
                            trayNumber: item.trayNumber,
                            stockPort: item.stockPort,
                            storageUserName: item.storageUserName,
                            storageTimeo: item.storageTimeo,
                            storageCompleteTime: item.storageCompleteTime,
                            qualityUserName: item.qualityUserName,
                            qualityTime: item.qualityTime,
                            qualityCompleteTime: item.qualityCompleteTime,
                            nodes: item.nodes

                        };
                    })
                };
            },
            page: true,
            limit: 40,
            limits: [40, 80, 120, 200, 500],
	    });
    };

    /**
     * 导出excel按钮
     */
    HxStorageOutDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageOutDetailed.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
	        var queryData = {};
	        queryData['timeType'] = $("#timeType").val();
	        queryData['startTime'] = $("#startTime").val();
	        queryData['endTime'] = $("#endTime").val();
	        queryData['customerId'] = $("#customerId").val();
	        queryData['setWarehouseId'] = $("#setWarehouseId").val();
	        queryData['areaName'] = $("#areaName").val();
	        queryData['positionNumber'] = $("#positionNumber").val();
	        queryData['getUnits'] = $("#getUnit").val();
	        queryData['condition'] = $("#condition").val();
	        queryData['associationOrder'] = $("#associationOrder").val();
	        queryData['goodsNames'] = $("#goodsName").val();
	        queryData['trayNumber'] = $("#trayNumber").val();
	        queryData['storageUserName'] = $("#storageUserName").val();
	        queryData['qualityUserName'] = $("#qualityUserName").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStorageOutDetailed/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    $.ajax({
        url:Feng.ctxPath + '/hxStorageOutDetailed/count',
        dataType:"json",   //返回格式为json
        async: false,
        type:"post",   //请求方式
        success:function(data){
            sl = data
        }
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxStorageOutDetailed.tableId,
        url: Feng.ctxPath + '/hxStorageOutDetailed/jilulist',

        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxStorageOutDetailed.initColumn()
        ,parseData: function(res){ // 数据转换
            return {
                "code": 0,
                "msg": res.msg, // 假设原数据中的msg字段存储的是提示信息
                "count": res.count,
                "data": $.map(res.data, function(item){ // 假设原数据中的data字段存储的是数据集
                    return {
                        // 假设你想转换的字段是numberField
                        realNum: Number(item.realNum), // 将字符串转换为数字
                        qualityNum: Number(item.qualityNum),
                        outWeight: Number(item.outWeight),
                        outVolume: Number(item.outVolume),
                        num: Number(item.num),
                        id: item.id,
                        setWarehouse: item.setWarehouse,
                        getUnito: item.getUnito,
                        areaName: item.areaName,
                        positionNumber: item.positionNumber,
                        storageNumber: item.storageNumber,
                        storageState: item.storageState,
                        startState: item.startState,
                        goodsName: item.goodsName,
                        goodsNumber: item.goodsNumber,
                        goodsPcode69: item.goodsPcode69,
                        goodsSpecs: item.goodsSpecs,
                        unit: item.unit,
                        outMoney: Number(item.outMoney),
                        detailedDate: item.detailedDate,
                        trayNumber: item.trayNumber,
                        stockPort: item.stockPort,
                        storageUserName: item.storageUserName,
                        storageTimeo: item.storageTimeo,
                        storageCompleteTime: item.storageCompleteTime,
                        qualityUserName: item.qualityUserName,
                        qualityTime: item.qualityTime,
                        qualityCompleteTime: item.qualityCompleteTime,
                        nodes: item.nodes

                    };
                })
            };
        },
        page: true,
        limit: 40,
        limits: [40, 80, 120, 200, 500],
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageOutDetailed.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxStorageOutDetailed.exportExcel();
    });
});
